package com.lcxw.hxzbapi.scheduleTask;

import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbcommon.common.StringUtil;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.annotation.Resource;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ClearVipScheduled {

    @Resource
    YwpdMemberService ywpdMemberService;


    /**
     * 每天0点开始清理过期vip用户
     */
    @SneakyThrows
    @Scheduled(cron = "0 0 0 * * ?")
    void clearVip() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = StringUtil.getDateTime();
        long now = sdf.parse(dateTime).getTime();
        List<YwpdMember> ywpdMembers = ywpdMemberService.selectVip();
        for (YwpdMember ywpdMember : ywpdMembers
        ) {
            String vipExpire = ywpdMember.getVipExpire();
            long vipExp = sdf.parse(vipExpire).getTime();
            if (vipExp <= now) {
                ywpdMember.setVip(0);
                ywpdMemberService.updateYwpdMember(ywpdMember);
            }
        }
    }
}
